/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.smsspamreport;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Helper class to manage French phone numbers
 * @author Christophe Petit<p>
 *
 */
public class FrenchPhoneNumberHelper {

	/**
	 * The list of the French premium-rates prefix phones numbers
	 */
	private static List<String> supiciousPrefixes=Arrays.asList("836","891","892","893","897","898","899");

	
	
	private FrenchPhoneNumberHelper() {
		super();
	}

	/**
	 * Extract all numbers in a string.<p>
	 * A number is a suit of numerics characters eventually prefixed with a '+' sign
	 * 
	 * @param text
	 * the text you want to extract numbers from.
	 * @return 
	 * All numbers founds in the text as String.
	 */
	public static List<String> extractsNumbers(String text) {
		ArrayList<String > res=new ArrayList<String>();
		String numbers = text.replaceAll( "[^\\+?+\\d$]", " " );
		StringTokenizer tokenizer=new StringTokenizer(numbers, " ", false);
		while(tokenizer.hasMoreElements()) {
			res.add(tokenizer.nextToken());
		}
		return res;
	}

	/**
	 * Check if a number is a valid French Number
	 * @param number
	 * The number to check as a string.
	 * @return 
	 * true if this number is a valid phone number.<br>
	 * false otherwise.
	 */
	public static String check(String number) {
		if (number == null) return null;
		int length=number.length();
		if (length!=10 && length != 12 && length != 13) return null;
		if (number.startsWith("+") && ! "33".equals(number.substring(1, 3))) return null;
		if (number.startsWith("00")&& ! "33".equals(number.substring(2, 4))) return null;
		if (number.startsWith("0033")) number="0"+number.substring(4);
		if (number.startsWith("+33")) number="0"+number.substring(3);
		//Now all numbers shoud start with 0XXXXXXXXX
		if (number.length()!=10) return null;
		return number;
	}

	
	/**
	 * Check if a number is a premium-rate number.<br>
	 * @param number
	 * A number (or not) eventually starting with +
	 * @return
	 * true if the number is a valid premium rate French number.<br>
	 * false if the number is not a valid French number
	 */
	public static boolean isSuspect(String number) {
		String validdatedNumber=check(number);
		if (validdatedNumber == null) {
			return false;
		}
		return supiciousPrefixes.contains((number.substring(1,4)));
	}
}
